﻿<%@ WebHandler Language="C#" Class="JSTest" %>

using System;
using System.Web;
using System.Collections;

using monadsphere.js;
using monadsphere.browser;
using monadsphere.ashx;

public class JSTest: JSHandler {

    public override void process(HttpContext context, Hashtable data) {

        JSNumber num = 10;

        HtmlWindow.alert(num + 5);
        
        // Array test
        JSString a = "a, b, c, d";
        JSArray<JSString> b = a.split(", ", 2);

        // for loop
        JSNumber i = new JSNumber(0);
        JSStmt.FOR(i < b.length, () => {
            HtmlWindow.alert("result: ", b[i]);
            i++;
        });

        // while loop
        i.set(0);
        JSStmt.WHILE(i < b.length, () => {
            HtmlWindow.alert(b[i]);
            i++;
        });

        // do while loop
        i.set(0);
        JSStmt.DO_WHILE(() => {
            HtmlWindow.alert(b[i]);
            i++;
        }, i < b.length);

        // object
        JSObject c = new JSObject();
        c["prop1"] = 1;
        c["prop2"] = 2;
        c["prop3"] = 3;

        // for ... each ... in loop
        JSNumber n = new JSNumber(0);
        JSNumber sum = new JSNumber(0);
        JSStmt.FOR_EACH_IN(n, c, () => {
            sum.set(sum + n);
        });
        HtmlWindow.alert(sum);

        JSObject d = new JSObject();
        c["prop1"] = "string1";
        c["prop2"] = "string2";
        c["prop3"] = "string3";

        JSString str = new JSString("");
        JSStmt.FOR_EACH_IN(str, c, () => {
            HtmlWindow.alert(str);
        });

        JSStmt.TRY_CATCH_FINALLY(()=> {
            HtmlWindow.alert("try");
        }, (JSException e)=> {
            HtmlWindow.alert("catch");
        }, ()=> {
            HtmlWindow.alert("finally");
        }
        );
    }
}
